package main

import (
	"github.com/micro/go-micro/v2"
	"log"
	"runtime"

	"kop/framework/conf"
	"kop/framework/wrapper"
	"kop/libs/database"
	"kop/modules/ring/handler"
	"kop/modules/ring/models"
	"kop/pb/service"
	log2 "kop/util/log"

	treasureH "kop/modules/treasure/handler"
	treasureM "kop/modules/treasure/models"
)

func main() {

	runtime.GOMAXPROCS(2)

	conf.Init()
	log2.SetLevel(conf.Log.Level)

	// New Service
	srv := micro.NewService(
		micro.Name("go.micro.srv.ring"),
		micro.WrapHandler(wrapper.NewLogWrapper),
		micro.AfterStart(
			func() error {
				log.Println("启动魔戒服务器.")
				return nil
			}),
	)

	// Initialise service
	srv.Init()

	database.DB = database.Open(conf.DataBase.Dsn, conf.DataBase.MaxIdle, conf.DataBase.MaxOpen, conf.DataBase.Log)
	defer database.DB.Close()

	models.AutoMigrate(database.DB)
	treasureM.AutoMigrate(database.DB)

	// Register handler
	_ = service.RegisterRingServiceHandler(srv.Server(), new(handler.RingServiceHandler))
	_ = service.RegisterTreasureServiceHandler(srv.Server(), new(treasureH.TreasureServiceHandler))

	if err := srv.Run(); err != nil {
		panic(err)
	}

}
